<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\activity\logic;

use plugin\saiadmin\basic\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\activity\model\activity;
use plugin\saiadmin\service\OpenSpoutWriter;

/**
 * 活动列表逻辑层
 */
class activityLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new activity();
    }
    public function export($where = [])
    {
        $query = $this->search($where);
        // ->field('id,name,enterprise_id,sort,status,create_time');
        $data = $this->getAll($query);
        // var_dump($data);exit();

        $file_name = '活动数据.xlsx';
        $header = [ '活动标题', '活动分类','活动开始时间', '活动结束时间','活动城市',
        '活动地点','活动封面','活动详情', '活动状态',
      '是否展示','创建时间'];
       
        $filter = [
            // 'activity_status' => [
            //     ['value' => 0, 'label' => '进行中'],
            //     ['value' => 1, 'label' => '已结束'],
            //     ['value' => 2, 'label' => '未开始']
            // ]
        ];
        $data_attr=[];
        foreach($data as $value){
           $data_attr[] = [
            'activity_title'=>$value['activity_title'],
            'activity_category_id_text'=>$value['activity_category_id_text'],
            'activity_start_time'=>$value['activity_start_time'],
            'activity_end_time'=>$value['activity_end_time'],
            'province'=>$value['province_id_text'].'-'.$value['city_id_text'].'-'.$value['district_id_text'],
            'activity_address'=>$value['activity_address'],
            'activity_cover'=>$value['activity_cover'],
            'activity_details'=>$value['activity_details'],
            'activity_status_text'=>$value['activity_status_text'],
            'is_display'=>$value['is_display']==1?'是':'否',
            'create_time'=>$value['create_time']
           
           ];
        }

        $writer = new OpenSpoutWriter($file_name);
        $writer->setWidth([20, 20, 20, 20, 20, 20,20,50,20,20,20]);
        $writer->setHeader($header);
        $writer->setData($data_attr, null, $filter);
        $file_path = $writer->returnFile();
        return response()->download($file_path, urlencode($file_name));
    }

}
